﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace DoAnHuongRung
{
    public partial class frmQuanLyNhanVien : Form
    {
        public frmQuanLyNhanVien()
        {
            InitializeComponent();
        }

        private void frmQuanLyNhanVien_Load(object sender, EventArgs e)
        {
            LoadChucVu();
            LoadDtgNhanVien();
            LoadDataTxtTimkiem();
        }

        private void LoadChucVu()
        {
            cmbChucVu.DisplayMember = "TenChucVu";
            cmbChucVu.ValueMember = "MaChucVu";
            cmbChucVu.DataSource = ChucVuBUS.SelectAll();
        }

        private void LoadDtgNhanVien()
        {
            dtgNhanVien.AutoGenerateColumns = false;
            List<NhanVienDTO> listnv = NhanVienBUS.SelectAll();
            dtgNhanVien.DataSource = listnv;
            for (int i = 0; i < listnv.Count(); i++)
            {
                if (listnv[i].GioiTinh.Equals("Nam"))
                    ((DataGridViewImageCell)dtgNhanVien.Rows[i].Cells[3]).Value = Properties.Resources.Nam;
                else
                    ((DataGridViewImageCell)dtgNhanVien.Rows[i].Cells[3]).Value = Properties.Resources.Nu;
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                NhanVienDTO nv = new NhanVienDTO();
                nv.HoTen = txtHoTen.Text;
                if (radioNam.Checked)
                    nv.GioiTinh = "Nam";
                else
                    nv.GioiTinh = "Nữ";
                nv.NgaySinh = dateTimePicker1.Value.ToShortDateString();
                nv.DiaChi = txtDiaChi.Text;
                nv.SoDienThoai = txtSoDienThoai.Text;
                nv.MaChucVu = int.Parse(cmbChucVu.SelectedValue.ToString());
                bool b = NhanVienBUS.Insert(nv);
                LoadDtgNhanVien();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                bool b = NhanVienBUS.Delete(int.Parse(dtgNhanVien.CurrentRow.Cells[0].Value.ToString()));
                LoadDtgNhanVien();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            try
            {
                NhanVienDTO nv = new NhanVienDTO();
                nv.MaNhanVien = int.Parse(dtgNhanVien.CurrentRow.Cells[0].Value.ToString());
                nv.HoTen = txtHoTen.Text;
                if (radioNam.Checked)
                    nv.GioiTinh = "Nam";
                else
                    nv.GioiTinh = "Nữ";
                nv.NgaySinh = dateTimePicker1.Value.ToShortDateString();
                nv.DiaChi = txtDiaChi.Text;
                nv.SoDienThoai = txtSoDienThoai.Text;
                nv.MaChucVu = int.Parse(cmbChucVu.SelectedValue.ToString());
                bool b = NhanVienBUS.Insert(nv);
                LoadDtgNhanVien();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnLamMoi_Click(object sender, EventArgs e)
        {
            LoadDtgNhanVien();
        }

        private void dtgNhanVien_SelectionChanged(object sender, EventArgs e)
        {
            txtHoTen.Text = dtgNhanVien.CurrentRow.Cells[1].Value.ToString();
            string gioitinh = dtgNhanVien.CurrentRow.Cells[2].Value.ToString();
            if (gioitinh.Equals("Nam"))
                radioNam.Checked = true;
            else
                radioNu.Checked = true;
            dateTimePicker1.Value = DateTime.Parse(dtgNhanVien.CurrentRow.Cells[4].Value.ToString());
            txtDiaChi.Text = dtgNhanVien.CurrentRow.Cells[5].Value.ToString();
            txtSoDienThoai.Text = dtgNhanVien.CurrentRow.Cells[6].Value.ToString();
            cmbChucVu.Text = dtgNhanVien.CurrentRow.Cells[7].Value.ToString();
        }

        private void LoadDataTxtTimkiem()
        {
            AutoCompleteStringCollection auto = new AutoCompleteStringCollection();
            List<NhanVienDTO> list = NhanVienBUS.SelectAll();
            for (int i = 0; i < list.Count(); i++)
                auto.Add(list[i].HoTen);
            txtTimKiem.AutoCompleteCustomSource = auto;

        }

        private void txtTimKiem_Click(object sender, EventArgs e)
        {
            if (txtTimKiem.Text.Equals("Tìm kiếm"))
            {
                txtTimKiem.Text = "";
                txtTimKiem.ForeColor = Color.Black;
            }
        }
    }
}
